package com.youmu.shop.cart.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youmu.shop.cart.dao.CartDao;
import com.youmu.shop.cart.domain.Cart;
import com.youmu.shop.cart.service.CartService;
import com.youmu.shop.cart.vo.CartVO;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;

	@Override
	public List<CartVO> findCart(Long userId) {
		return cartDao.findCart(userId);
	}

	@Override
	public long saveCart(Cart cart) {
	    //查询购物车里是否有重复的商品
	    Long cartId = cartDao.checkIfRepeat(cart);
	    //若有重复，则根据cartId获取原来的购物车项oldCart，数量修改为加cart.getQuantity()  + oldCart.getQuantity()
	    if(cartId != null){
	        Cart oldCart = (Cart)cartDao.getById(cartId);
	        cartDao.update(new Cart(cartId,oldCart.getNumber()+cart.getNumber()));
	        return cartId;
	    }else{
	        return cartDao.save(cart);
	    }
	}

	@Override
	public void updateCart(Cart cart) {
		cartDao.update(cart);
	}

	@Override
	public void deleteCart(Long id) {
		cartDao.update(new Cart(id,true));
	}

    






}
